package qst.com.bacK.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.mapper.DimensionMapper;
import qst.com.bacK.mapper.QuestionMapper;
import qst.com.bacK.pojo.entity.PersonalityDimension;
import qst.com.bacK.service.DimensionService;

import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/11 下午4:36
 * Description:
 */

@Service
public class DimensionServiceImpl implements DimensionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private DimensionMapper dimensionMapper;

    @Override
    public List<PersonalityDimension> findDimensionByAssessment(int id) {
        return dimensionMapper.findByAssessment(id);
    }

    @Override
    public void deleteDimension(int id) {
        int count = questionMapper.findCountByDimension(id);
        if (count > 0) {
            throw new ExamException("本知识点已录入考题，不能再删除");
        }
        dimensionMapper.delete(id);
    }

    @Override
    public PersonalityDimension findDimensionById(int id) {
        return dimensionMapper.findById(id);
    }

    @Override
    public void saveDimension(PersonalityDimension kp) {
        PersonalityDimension temp = dimensionMapper.findByAssessmentAndTitle(kp.getAssessmentId(), kp.getTitle());
        if (temp == null) {
            dimensionMapper.insert(kp);

        } else {
            throw new ExamException("本知识点名称已存在");
        }
    }

    @Override
    public void updateDimension(PersonalityDimension kp) {
        PersonalityDimension temp = dimensionMapper.findByAssessmentAndTitle(kp.getAssessmentId(), kp.getTitle());
        if (temp == null || temp.getId().equals(kp.getId())) {
            dimensionMapper.update(kp);
        } else {
            throw new ExamException("本知识点名称已存在");
        }
    }
}
